from ckeditor_uploader.fields import RichTextUploadingField
from django.db import models
from django.db.models import Q
from app.models import User
from django.utils import timezone


# 博客分类
class Category(models.Model):
    # 标题
    title = models.CharField(max_length=100, blank=True, db_column='标题', verbose_name='标题')
    # 创建时间
    createdTime = models.DateTimeField(auto_now=True, db_column='创建时间', verbose_name='创建时间')

    # admin站点显示,调查查看对象方便
    def __str__(self):
        return self.title

    # 被使用的次数
    # used_count = models.IntegerField(default=0, db_column='被使用的次数', verbose_name='被使用的次数')

    class Meta:
        db_table = '文章分类'  # 修改表名
        verbose_name_plural = verbose_name = db_table
        # ordering = ['-used_count']

    @classmethod
    def get_all(cls):
        return cls.objects.values('title')

    @classmethod
    def get_by_title(cls, title):
        category_obj = Category.objects.filter(title=title).first()
        if not category_obj:  # 如果数据库中没有该类,则新建该类
            category_obj = Category()
            category_obj.title = title
            category_obj.save()
        return category_obj


# 标签
class Tag(models.Model):
    # title
    title = models.CharField(max_length=20, blank=True, db_column='标签', verbose_name='标签')

    # 创建时间
    create_time = models.DateTimeField(auto_now_add=True, db_column='创建时间', verbose_name='创建时间')

    # creater
    creater = models.ForeignKey(User, db_column='创建人', verbose_name='创建人', on_delete=models.CASCADE)

    # 被使用的次数

    class Meta:
        db_table = '标签'
        verbose_name_plural = verbose_name = db_table

    def __str__(self):
        return self.title

    @classmethod
    def get_all(cls):
        return cls.objects.values('title')


# 博客
class Blog(models.Model):
    # 作者
    author = models.ForeignKey(User, on_delete=models.CASCADE, db_column='作者', verbose_name='作者')

    # 标题
    title = models.CharField(max_length=50, blank=True, db_column='标题', verbose_name='标题')

    # 分类
    category = models.ForeignKey(Category, on_delete=models.CASCADE, db_column='分类', verbose_name='分类')

    # 标签
    tags = models.CharField(max_length=50, blank=True, db_column='标签_', verbose_name='标签_')

    # 标签 一篇博客对应多个标签 一个标签对应多个博客
    tag = models.ManyToManyField(Tag, db_column='标签', verbose_name='标签')

    # 摘要信息
    summary = models.CharField(max_length=200, null=False, blank=False, db_column='摘要信息', verbose_name='摘要信息')

    # 文章正文
    # content = models.TextField(db_column='文章正文', verbose_name='文章正文')
    content = RichTextUploadingField(db_column='文章正文', verbose_name='文章正文')

    # 浏览量
    total_views = models.PositiveIntegerField(default=0, db_column='浏览量', verbose_name='浏览量')

    # 获赞量
    total_likes = models.PositiveIntegerField(default=0, db_column='获赞量', verbose_name='获赞量')

    # 收藏量
    total_collect = models.IntegerField(default=0, db_column='收藏量', verbose_name='收藏量')

    # 文章创建时间
    createdTime = models.DateTimeField(default=timezone.now, db_column='文章创建时间', verbose_name='文章创建时间')

    # 文章修改的时间
    updatedTime = models.DateTimeField(db_column='文章修改的时间', verbose_name='文章修改的时间')  # 自动添加auto_now=True,

    # 是否显示本文
    isShow = models.BooleanField(default=True, db_column='是否显示本文', verbose_name='是否显示本文')
    # 是否置顶
    isTop = models.BooleanField(default=False, db_column='是否置顶', verbose_name='是否置顶')

    # 附件
    connectedFile1 = models.FileField(upload_to='file/%Y/%m/%d', db_column='附件1', blank=True)
    connectedFile2 = models.FileField(upload_to='file/%Y/%m/%d', db_column='附件2', blank=True)
    connectedFile3 = models.FileField(upload_to='file/%Y/%m/%d', db_column='附件3', blank=True)
    connectedFile4 = models.FileField(upload_to='file/%Y/%m/%d', db_column='附件4', blank=True)
    connectedFile5 = models.FileField(upload_to='file/%Y/%m/%d', db_column='附件5', blank=True)

    class Meta:
        db_table = '博客'  # 修改表名
        verbose_name = verbose_name_plural = db_table
        ordering = ['-isTop', '-createdTime']

    def __str__(self):
        return self.title

    @classmethod
    def get_all(cls):
        return cls.objects.filter(isShow=1)

    @classmethod
    def get_by_id(cls, id):
        return cls.objects.filter(id=id).first()

    @classmethod
    def update(cls, blog_obj, title, author_id, category_id, tags, content, summary, updatedTime, isShow,
               connectedFiles):
        blog_obj.title = title
        blog_obj.author_id = author_id
        blog_obj.category_id = category_id
        blog_obj.tags = tags
        blog_obj.content = content
        blog_obj.summary = summary
        blog_obj.updatedTime = updatedTime
        blog_obj.isShow = isShow
        blog_obj.connectedFile1, blog_obj.connectedFile2, blog_obj.connectedFile3, blog_obj.connectedFile4, blog_obj.connectedFile5 = connectedFiles
        blog_obj.save()
        return blog_obj


    @classmethod
    def get_count_by_category_id(cls,category_id):
        return cls.objects.filter(category_id=category_id).count()

# 用户评论表
class JudgeTable(models.Model):
    # 评论人的id
    judger = models.ForeignKey(User, on_delete=models.CASCADE, db_column='评论人', verbose_name='评论人')
    # 评论的博客的id
    judgeBlog = models.ForeignKey(Blog, on_delete=models.CASCADE, db_column='被评论的博客', verbose_name='被评论的博客')

    # 评论内容
    content = models.CharField(max_length=500, db_column='评论内容', verbose_name='评论内容')

    # 评论时间
    judgeTime = models.DateTimeField(auto_now_add=True, db_column='评论时间', verbose_name='评论时间')

    # 是否显示 如果用户删除评论后 该评论就不被显示
    isShow = models.BooleanField(default=True, db_column='是否显示', verbose_name='是否显示')

    class Meta:
        db_table = '评论'
        verbose_name = verbose_name_plural = db_table
        ordering = ['judgeTime']

    # 根据博客id获取该博客下的评论量
    @classmethod
    def get_judge_count_by_blog_id(cls, blog_id):
        return cls.objects.filter(Q(judgeBlog_id=blog_id) & Q(isShow=True)).count()


# 收藏记录表
class CollectTable(models.Model):
    # 收藏人的id
    collectorId = models.ForeignKey(User, on_delete=models.CASCADE, db_column='收藏人', verbose_name='收藏人')

    # 被收藏的博客的id
    blogId = models.ForeignKey(Blog, on_delete=models.CASCADE, db_column='被收藏的博客', verbose_name='被收藏的博客')

    # 是否收藏
    isCollected = models.BooleanField(default=0, db_column='是否被收藏', verbose_name='是否被收藏')

    # 收藏时的日期
    collectTime = models.DateField(auto_now_add=True, db_column='收藏日期', verbose_name='收藏日期')

    class Meta:
        db_table = '收藏'
        verbose_name = verbose_name_plural = db_table
        ordering = ['-collectTime']

    # 判断某一篇文章是否被某一个用户收藏了
    @classmethod
    def is_collected(cls, userId, blogid):
        try:
            return cls.objects.filter(Q(collectorId=userId) & Q(blogId=blogid)).first().isCollected
        except:
            return ''


# 搜索记录表
class SearchTable(models.Model):
    # 关键字
    keyword = models.CharField(max_length=100, db_column='关键字', verbose_name='关键字')

    # 搜索的日期
    createdTime = models.DateTimeField(default=timezone.now, db_column='搜索的日期', verbose_name='搜索日期')

    class Meta:
        db_table = '搜索记录'
        verbose_name = verbose_name_plural = db_table
        ordering = ['-createdTime']


# 推荐
class Recommend(models.Model):
    tilte = models.ForeignKey(Blog, on_delete=models.CASCADE, db_column='博客', verbose_name='博客')

    weight = models.PositiveIntegerField(default=0, db_column='权重', verbose_name='权重', help_text='权重越大，排名越靠前')

    class Meta:
        db_table = '推荐'
        verbose_name = verbose_name_plural = db_table
        ordering = ['-weight']

    @classmethod
    def get_all(cls):
        return cls.objects.all()
